/* 
 * BaseAction.java
 * 
 * Created on 2007-4-12
 * 
 * Copyright(C) 2007, by Ambow Research&Development Branch.
 * 
 * Original Author: Peng Qing
 * Contributor(s):
 * 
 * Changes 
 * -------
 * $Log: BaseAction.java,v $
 * Revision 1.2  2008/03/06 09:03:09  gaochao
 * *** empty log message ***
 *
 * Revision 1.1  2007/12/18 02:03:23  wanglirong
 * *** empty log message ***
 *
 * Revision 1.1  2007/07/19 06:53:09  pengqing
 * *** empty log message ***
 *
 * Revision 1.1  2007/04/07 18:37:40  yaoshunxiang
 * *** empty log message ***
 *
 * Revision 1.4  2007/05/16 07:42:45  yaoshunxiang
 * *** empty log message ***
 *
 * Revision 1.3  2007/04/27 01:39:55  zhangrui
 * add<code>setRequestAttribute(String,Object)</code>;
 * add<code>getSessionObj(Object)</code>;
 * add<code>setSessionObj(Object,Object)</code>
 *
 * Revision 1.2  2007/04/24 01:51:17  pengqing
 * add getSession method
 *
 * Revision 1.1  2007/04/12 10:08:59  pengqing
 * Webwork Action base
 *
 *
 */
package com.ambow.core.web.action;

import java.io.File;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.ambow.core.dao.support.Page;
import com.ambow.core.util.UrlUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public abstract class BaseAction extends ActionSupport {
	
	/** 新增页面名： */
	protected final static String ADD = "add";
	
	/** 修改页面名： */
	protected final static String EDIT = "edit";
		
	public HttpServletRequest getRequest() {
		HttpServletRequest request = ServletActionContext.getRequest();
		return request;
	}
	public  HttpServletResponse getResponse() {
		HttpServletResponse response = ServletActionContext.getResponse();
		return response;
	}
	
	/**
	 * 获得当前Action 请求路径
	 * 
	 */
	public String getRequestUrl() {
		return UrlUtils.getRequestUrl(getRequest());
	}

	/**
	 * 获得当前Action 请求的全部路径
	 */
	public String getFullRequestUrl() {
		return UrlUtils.getFullRequestUrl(getRequest());
	}

	/**
	 * 获得当前Session
	 * 
	 */
	public Map getSession() {
		return ActionContext.getContext().getSession();
	}

	/**
	 * 根据指定的页面参数名称，获取页面传递来的参数值
	 * 
	 * @param parameter
	 * @return 单个对象
	 */
	protected Object getParameterValue(String parameter) {
		Object[] parameterArray = getParamenterArray(parameter);
		if (parameterArray != null && parameterArray.length == 1) {
			return parameterArray[0];
		} else {
			return getRequest().getAttribute(parameter);
		}
	}

	/**
	 * 根据指定的页面参数名称，获取页面传递来的参数值
	 * 
	 * @param parameter
	 * @return 数组对象
	 */
	protected Object[] getParamenterArray(String parameter) {
		return (Object[]) (ActionContext.getContext().getParameters()
				.get(parameter));
	}

	
	
	/**
	 * 向request对象添加attribute
	 * 
	 * @param key
	 * @param value
	 */
	public void setRequestAttribute(String key, Object value) {
		getRequest().setAttribute(key, value);
	}

	/**
	 * 从session中取得相应的值
	 * 
	 * @param key
	 * @return
	 */
	public Object getSessionObj(Object key) {
		return getSession().get(key);
	}
	
	/**
	 * 获得项目rootDir
	 * 
	 * @return
	 */
	protected String getRootDir() {
		String workDir = getRequest().getSession()
				.getServletContext().getRealPath("login.jsp");
		int len = workDir.indexOf("login.jsp");
		workDir = workDir.substring(0, len);

		StringBuffer sb = new StringBuffer();
		sb.append(workDir).append("upload").append(File.separator);

		return sb.toString();
	}
	
	@SuppressWarnings("unchecked")
	public void setSessionObj(Object key,Object value){
		getSession().put(key, value);
	}
	
	private Page page;

	public Page getPage() {
		return page;
	}


	public void setPage(Page page) {
		this.page = page;
	}
	
	private int pageNo;

	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	
}
